import { reqAddOrUpdataShopCart, reqGetProInfo } from '../api'
import {getUUID} from '@/utils/uuid_token'
const state = {
  getproInfo: {},
  uuid_token:getUUID()
}
const mutations = {
  GETPROINFO (state, getproInfo) {
    state.getproInfo = getproInfo
  }
}
const actions = {
  // 获取产品信息的action
  async getproInfo ({ commit }, skuId) {
    let result = await reqGetProInfo(skuId)
    if (result.code == 200) {
      commit('GETPROINFO', result.data)
    }
  },
  // 将产品添加到购物车中
  async addOrUpdataShopCart ({ commit }, { skuId, skuNum }) {
    let result = await reqAddOrUpdataShopCart(skuId, skuNum)
    if(result.code==200){
      return "ok";
    }else{
      return Promise.reject(new Error("faile"))
    }
  }
}

const getters = {
  // state.getproInfo初始状态为空对象{},空对象的categoryView属性值为undefined
  // 当前计算出的categoryView属性值至少是一个空对象，假的会报undefined

  // 产品面包屑
  categoryView (state) {
    return state.getproInfo.categoryView || {}
  },
  // 产品信息
  skuInfo (state) {
    return state.getproInfo.skuInfo || {}
  },
  spuSaleAttrList (state) {
    return state.getproInfo.spuSaleAttrList || {}
  }

}

export default {
  state, actions, mutations, getters
}

